home *** CD-ROM | disk | FTP | other *** search
/ PC Format (PL) 2008 February / PC_Format_022008.iso / Internet / Mozilla Thunderbird wtyczki / lightning-0.7-tb-win.xpi / js / calEvent.js < prev    next >
Encoding:
JavaScript  |  2007-08-30  |  9.2 KB  |  286 lines

  1. /* -*- Mode: javascript; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is Oracle Corporation code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  *  Oracle Corporation
  19.  * Portions created by the Initial Developer are Copyright (C) 2004
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
  24.  *   Mike Shaver <shaver@off.net>
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  28.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. //
  41. // calEvent.js
  42. //
  43.  
  44. //
  45. // constructor
  46. //
  47. function calEvent() {
  48.     this.wrappedJSObject = this;
  49.     this.initItemBase();
  50.  
  51.     this.eventPromotedProps = {
  52.         "DTSTART": true,
  53.         "DTEND": true,
  54.         "DTSTAMP": true,
  55.         __proto__: this.itemBasePromotedProps
  56.     }
  57. }
  58.  
  59. // var trickery to suppress lib-as-component errors from loader
  60. var calItemBase;
  61.  
  62. var calEventClassInfo = {
  63.     getInterfaces: function (count) {
  64.         var ifaces = [
  65.             Components.interfaces.nsISupports,
  66.             Components.interfaces.calIItemBase,
  67.             Components.interfaces.calIEvent,
  68.             Components.interfaces.calIInternalShallowCopy,
  69.             Components.interfaces.nsIClassInfo
  70.         ];
  71.         count.value = ifaces.length;
  72.         return ifaces;
  73.     },
  74.  
  75.     getHelperForLanguage: function (language) {
  76.         return null;
  77.     },
  78.  
  79.     contractID: "@mozilla.org/calendar/event;1",
  80.     classDescription: "Calendar Event",
  81.     classID: Components.ID("{974339d5-ab86-4491-aaaf-2b2ca177c12b}"),
  82.     implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
  83.     flags: 0
  84. };
  85.  
  86. calEvent.prototype = {
  87.     __proto__: calItemBase ? (new calItemBase()) : {},
  88.  
  89.     QueryInterface: function (aIID) {
  90.         if (aIID.equals(Components.interfaces.calIEvent) ||
  91.             aIID.equals(Components.interfaces.calIInternalShallowCopy))
  92.             return this;
  93.  
  94.         if (aIID.equals(Components.interfaces.nsIClassInfo))
  95.             return calEventClassInfo;
  96.  
  97.         return this.__proto__.__proto__.QueryInterface.call(this, aIID);
  98.     },
  99.  
  100.     cloneShallow: function (aNewParent) {
  101.         var m = new calEvent();
  102.         this.cloneItemBaseInto(m, aNewParent);
  103.  
  104.         return m;
  105.     },
  106.  
  107.     clone: function () {
  108.         var m;
  109.  
  110.         if (this.mParentItem) {
  111.             var clonedParent = this.mParentItem.clone();
  112.             m = clonedParent.recurrenceInfo.getOccurrenceFor (this.recurrenceId);
  113.         } else {
  114.             m = this.cloneShallow(null);
  115.         }
  116.  
  117.         return m;
  118.     },
  119.  
  120.     createProxy: function () {
  121.         if (this.mIsProxy) {
  122.             calDebug("Tried to create a proxy for an existing proxy!\n");
  123.             throw Components.results.NS_ERROR_UNEXPECTED;
  124.         }
  125.  
  126.         var m = new calEvent();
  127.         m.initializeProxy(this);
  128.  
  129.         return m;
  130.     },
  131.  
  132.     makeImmutable: function () {
  133.         this.makeItemBaseImmutable();
  134.     },
  135.  
  136.     get duration() {
  137.         return this.endDate.subtractDate(this.startDate);
  138.     },
  139.  
  140.     get recurrenceStartDate() {
  141.         return this.startDate;
  142.     },
  143.  
  144.     icsEventPropMap: [
  145.     { cal: "DTSTART", ics: "startTime" },
  146.     { cal: "DTEND", ics: "endTime" }],
  147.  
  148.     set icalString(value) {
  149.         this.icalComponent = icalFromString(value);
  150.     },
  151.  
  152.     get icalString() {
  153.         const icssvc = Components.
  154.           classes["@mozilla.org/calendar/ics-service;1"].
  155.           getService(Components.interfaces.calIICSService);
  156.         var calcomp = icssvc.createIcalComponent("VCALENDAR");
  157.         calcomp.prodid = "-//Mozilla Calendar//NONSGML Sunbird//EN";
  158.         calcomp.version = "2.0";
  159.         if (this.hasProperty("METHOD")) {
  160.             calcomp.method = this.getProperty("METHOD");
  161.         }
  162.         calcomp.addSubcomponent(this.icalComponent);
  163.         return calcomp.serializeToICS();
  164.     },
  165.  
  166.     get icalComponent() {
  167.         const icssvc = Components.
  168.           classes["@mozilla.org/calendar/ics-service;1"].
  169.           getService(Components.interfaces.calIICSService);
  170.         var icalcomp = icssvc.createIcalComponent("VEVENT");
  171.         this.fillIcalComponentFromBase(icalcomp);
  172.         this.mapPropsToICS(icalcomp, this.icsEventPropMap);
  173.         
  174.         var bagenum = this.mProperties.enumerator;
  175.         while (bagenum.hasMoreElements()) {
  176.             var iprop = bagenum.getNext().
  177.                 QueryInterface(Components.interfaces.nsIProperty);
  178.             try {
  179.                 if (!this.eventPromotedProps[iprop.name]) {
  180.                     var icalprop = icssvc.createIcalProperty(iprop.name);
  181.                     icalprop.value = iprop.value;
  182.                     var propBucket = this.mPropertyParams[iprop.name];
  183.                     if (propBucket) {
  184.                         for (paramName in propBucket) {
  185.                             icalprop.setParameter(paramName,
  186.                                                   propBucket[paramName]);
  187.                         }
  188.                     }
  189.                     icalcomp.addProperty(icalprop);
  190.                 }
  191.             } catch (e) {
  192.                 dump("XXX failed to set " + iprop.name + " to " + iprop.value +
  193.                 ": " + e + "\n");
  194.             }
  195.         }
  196.         return icalcomp;
  197.     },
  198.  
  199.     eventPromotedProps: null,
  200.  
  201.     set icalComponent(event) {
  202.         this.modify();
  203.         if (event.componentType != "VEVENT") {
  204.             event = event.getFirstSubcomponent("VEVENT");
  205.             if (!event)
  206.                 throw Components.results.NS_ERROR_INVALID_ARG;
  207.         }
  208.  
  209.         this.setItemBaseFromICS(event);
  210.         this.mapPropsFromICS(event, this.icsEventPropMap);
  211.  
  212.         this.importUnpromotedProperties(event, this.eventPromotedProps);
  213.         
  214.         // If there is a duration set on the event, calculate the right
  215.         // end time.
  216.         // XXX This means that serializing later will loose the duration
  217.         //     information, to replace it with a dtend. bug 317786
  218.         if (event.duration) {
  219.             this.endDate = this.startDate.clone();
  220.             this.endDate.addDuration(event.duration);
  221.         }
  222.         
  223.         // If endDate is still invalid neither a end time nor a duration is set
  224.         // on the event. We have to set endDate ourselves.
  225.         // If the start time is a date-time the event ends on the same calendar
  226.         // date and time of day. If the start time is a date the events
  227.         // non-inclusive end is the end of the calendar date.
  228.         var endDate = this.endDate;
  229.         if (!endDate || !endDate.isValid) {
  230.             this.endDate = this.startDate.clone();
  231.             if (this.startDate.isDate) {
  232.                 this.endDate.day += 1;
  233.             }
  234.         }
  235.         
  236.         // Importing didn't really change anything
  237.         this.mDirty = false;
  238.     },
  239.  
  240.     isPropertyPromoted: function (name) {
  241.         return (this.eventPromotedProps[name]);
  242.     },
  243.  
  244.     getOccurrencesBetween: function(aStartDate, aEndDate, aCount) {
  245.         if (this.recurrenceInfo) {
  246.             return this.recurrenceInfo.getOccurrences(aStartDate, aEndDate, 0, aCount);
  247.         }
  248.  
  249.         if (checkIfInRange(this, aStartDate, aEndDate)) {        
  250.             aCount.value = 1;
  251.             return [this];
  252.         }
  253.  
  254.         aCount.value = 0;
  255.         return [];
  256.     },
  257.  
  258.     set startDate(value) {
  259.         this.modify();
  260.  
  261.         // We're about to change the start date of an item which probably
  262.         // could break the associated calIRecurrenceInfo. We're calling
  263.         // the appropriate method here to adjust the internal structure in
  264.         // order to free clients from worrying about such details.
  265.         if (this.parentItem == this) {
  266.             var rec = this.recurrenceInfo;
  267.             if (rec) {
  268.                 rec.onStartDateChange(value,this.startDate);
  269.             }
  270.         }
  271.  
  272.         this.setProperty("DTSTART", value);
  273.     },
  274.  
  275.     get startDate() {
  276.         return this.getProperty("DTSTART");
  277.     }
  278. };
  279.  
  280. // var to avoid spurious errors when loaded as component during autoreg
  281.  
  282. var makeMemberAttr;
  283. if (makeMemberAttr) {
  284.     makeMemberAttr(calEvent, "DTEND", null, "endDate", true);
  285. }
  286.